define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    if(Config.accounturl == '' || Config.accounturl == null){
        Config.accounturl = 0;
    }
    if(Config.timedate == '' || Config.timedate == null){
        Config.timedate = 0;
    }
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'feditionstatisticsshopify/index/?accounturl=' + Config.accounturl+'&timedate=' + Config.timedate,
                    add_url: 'feditionstatisticsshopify/add',
                    edit_url: 'feditionstatisticsshopify/edit',
                    del_url: 'feditionstatisticsshopify/del',
                    multi_url: 'feditionstatisticsshopify/multi',
                    import_url: 'feditionstatisticsshopify/import',
                    table: 'fedition_statisticsshopify',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'daystamp',
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'accounturl', title: __('Accounturl'),visible: false,addClass: "selectpage", extend: "data-source='feditionaccounturl/index' data-field='name'"},
                        {field: 'accounturl_name', title: __('Accounturl'), operate: false},
                        {field: 'daystamp', title: __('Day'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, datetimeFormat:"YYYY-MM-DD"},
                        {field: 'sum', title: __('Sum'), operate: 'LIKE'},
                        {field: 'orders', title: __('订单数'), operate: 'LIKE'},
                        // {field: 'refunded', title: __('退款金额'), operate: 'LIKE'},
                        {field: 'refunded_orders', title: __('退款订单数'), operate: 'LIKE'},
                        {field: 'gateway', title: __('支付方式'), formatter:Table.api.formatter.flag, operate: 'LIKE',custom:{'shopify_payments':'info','paypal':'warning'}},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('接入shopify订单明细'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('收款订单'),
                                    title: __('收款订单'),
                                    extend: 'data-area=\'["90%","90%"]\'',
                                    classname: 'btn btn-sm btn-success btn-dialog',
                                    icon: 'fa fa-usd',
                                    url: 'feditionordershopify/index/?accounturl={accounturl}&daystamp={daystamp}&gateway={gateway}&type=1'
                                },
                                {
                                    name: 'detail',
                                    text: __('退款订单'),
                                    title: __('退款订单'),
                                    extend: 'data-area=\'["90%","90%"]\'',
                                    classname: 'btn btn-sm btn-info btn-dialog',
                                    icon: 'fa fa-usd',
                                    url: 'feditionordershopify/index/?accounturl={accounturl}&daystamp={daystamp}&gateway={gateway}&type=2'
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                $("#zsum").text(data.Statistics.zsum);
                $("#zorders").text(data.Statistics.zorders);
                $("#zrefunded").text(data.Statistics.zrefunded);
                $("#zrefunded_orders").text(data.Statistics.zrefunded_orders);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#c-accounturl").data("formatItem",function (item) {
                return "店铺名称:"+item.name+",店铺域名:"+item.url;
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $("#c-accounturl").data("formatItem",function (item) {
                return "店铺名称:"+item.name+",店铺域名:"+item.url;
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});